package com.babyshop.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.babyshop.domain.Manufacturer;
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class ManufacturerDaoImpl implements ManufacturerDao {

	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	@Override
	public void saveOfUpdate(Manufacturer manufacturer) {
		try {
			session.saveOrUpdate(manufacturer);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public void delete(int id) {
		try {
			Manufacturer manufacturer = (Manufacturer) session.get(Manufacturer.class, id);
			session.delete(manufacturer);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}

	}

	@Override
	public Manufacturer get(int id) {
		Manufacturer manufacturer = null;
		try {
			manufacturer = (Manufacturer) session.get(Manufacturer.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return manufacturer;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Manufacturer> getAll() {
		List<Manufacturer> listManufacturers = null;
		try {
			listManufacturers = session.createQuery("from Manufacturer").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listManufacturers;
	}
}
